<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="720px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改代理' : '添加代理'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="代理账号:" prop="oemCode">
            <el-input
              clearable
              :maxlength="11"
              :disabled="isUpdate"
              v-model="form.oemCode"
              placeholder="请输入代理账号"
            />
          </el-form-item>
          <!--<el-form-item label="平台详情" prop="platformDetails">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.platformDetails"
              placeholder="请输入平台名称"
            />
          </el-form-item>-->

          <el-form-item label="代理类型:" prop="agencyId">
            <el-select
              clearable
              class="ele-block"
              v-model="form.agencyId"
              placeholder="请选择代理类型"
            >
              <el-option v-for="(i,j) in typeList" :key="j" :label="i.agencyName" :value="i.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="到期时间:" prop="expirationTime">
            <el-date-picker
              style="width: 100%;"
              v-model="form.expirationTime"
              :picker-options="pickerOptions"
              type="date"
              placeholder="请选择到期时间"
              @change="selTime">
            </el-date-picker>
          </el-form-item>
          <!-- <el-form-item label="分佣比例:" prop="commissionSharingRatio">
             <el-input
               clearable
               v-model.number="form.commissionSharingRatio"
               placeholder="请输入分佣比例"
             />
           </el-form-item>-->
        </el-col>
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="密码:" prop="oemPassword" v-if="!data">
            <el-input
              clearable
              :maxlength="20"
              :min="6"
              show-password
              v-model="form.oemPassword"
              placeholder="请输入密码"
            />
          </el-form-item>
          <el-form-item label="联系人" prop="linkman">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.linkman"
              placeholder="请输入联系人"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input
              maxlength="11"
              v-model="form.phone"
              clearable
              placeholder="请输入联系电话"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { emailReg, phoneReg } from 'ele-admin';
  import RoleSelect from './role-select.vue';
  import ActivityEdit from './activity-edit.vue';
  import { addUser, updateUser, checkExistence } from '@/api/oem';
  import {JSEncrypt} from "jsencrypt";

  export default {
    components: { RoleSelect,ActivityEdit},
    props: {
      // 代理类型
      typeList: Array,
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        expirationTime:'',
        id:'',
        oemCode: '',
        commissionSharingRatio:'',
        oemPassword: '',
        platformDetails: '',
        agencyId:'',
        linkman:'',
        phone:'',
        oemType:'2',
        proxyAddress:'',
      };
      return {
        // 日期设置对象
        pickerOptions: {
          disabledDate(time) {
            // return time.getTime() < Date.now() - 8.64e7;   //禁用以前的日期，今天不禁用
            return time.getTime() <= Date.now();    //禁用今天以及以前的日期
          }
        },
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          agencyId: [
            {
              required: true,
              message: '请输入代理类型',
              trigger: 'blur'
            }
          ],
          oemCode: [
            {
              required: true,
              // message: '请输入代理账号',
              trigger: 'blur',
              message: '请输入11位数的代理手机号',
            },
            {
              pattern: /^1[3456789]\d{9}$/,
              message: '请输入正确的代理手机号',
              trigger: 'change'
            }
            /* {
               // pattern:/^\S{6,}$/,
               pattern:/[a-zA-Z0-9]{6,11}$/,
               message: '请输入6-11位数的代理账号(仅字母和数字)'
             }*/
          ],
          commissionSharingRatio: [
            {
              required: true,
              // message: '请输入代理账号',
              trigger: 'blur',
              message: '请输入分佣比例',
            },
            {
              pattern:/^([1-9]\d{0,1}|100)$/,
              message: '请输入1-100的整数'
            }
          ],
          expirationTime: [
            {
              required: true,
              message: '请选择到期时间',
              trigger: 'blur'
            }
          ],
          oemPassword: [
            {
              required: true,
              pattern: /^[\S]{6,18}$/,
              message: '密码必须为6-18位非空白字符',
              // message: '请输入密码',
              trigger: 'blur'
            }
          ],
          proxyAddress: [
            {
              required: true,
              message: '请输入域名地址',
              trigger: 'blur'
            }
          ],
          phone: [
            {
              required: true,
              message: '请输入联系人电话',
              // message: this.form.phone ? '请输入合法的电话' : '请输入联系人电话',
              trigger: 'blur'
            },
            {
              pattern:/^1[3,4,5,6,7,8,9][0-9]{9}$/,
              message: '请输入正确的电话号码',
            }
          ],
          linkman: [
            {
              required: true,
              message: '请输入联系人',
              trigger: 'blur'
            }
          ],
          counts: [
            {
              required: true,
              message: '请输入联系人',
              trigger: 'blur'
            }
          ],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      // 选择到期时间
      selTime(e){
        console.log('selTime=======',e)
        // msToDate: 中国标准时间转化为年月日
        this.form.expirationTime= this.$sun.msToDate(e)
        console.log('form.expirationTime',this.form.expirationTime)
      },
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          this.form.parentId=this.$store.state.user.info.oemId
          const data = {
            ...this.form,
          };
          const saveOrUpdate = this.isUpdate ? updateUser : addUser;
          if (!this.isUpdate) {
            let encryptor = new JSEncrypt()
            let pk = this.$store.state.signature.pk
            console.log("pk",pk)
            encryptor.setPublicKey(pk)
            data.oemPassword = encryptor.encrypt(data.oemPassword)
          }
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              let hint = this.isUpdate ? '修改代理成功！' : '添加代理成功！'
              this.$message.success(hint);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);``
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            console.log("this.data",this.data)
            this.$util.assignObject(this.form, {
              ...this.data,
              password: null,
              sex: this.data.sex + ''
            });
            this.isUpdate = true;
          } else {
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
